package cn.jiedh.app.meiweidk.Utils.tool;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;

/**
 * @name：获取经纬度相关数据的工具类
 * @author：司马林
 * @phone：18423134135
 * @createTime：2017/10/26
 */

public class LocationUtil {

    public static Double[] location = new Double[]{new Double(39.56323252232),new Double(106.65252652525)};
    public static Double[] getGpsLocation(Context context, LocationManager locationManager) {
        Double lat = null;
        Double lng = null;
        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                    && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                return null;
            }
            Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if (location != null) {
                lat = location.getLatitude();
                lng = location.getLongitude();
                L.e("Gps：" + "lat = " + lat + "\nlng = " + lng);
            } else {
                return getNetWorkLocation(context, locationManager);
            }
        }
        return new Double[]{lat, lng};
    }

    public static Double[] getNetWorkLocation(Context context, LocationManager locationManager) {
        Double lat = null;
        Double lng = null;
        LocationListener locationListener = new LocationListener() {

            // Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
            @Override
            public void onStatusChanged(String provider, int status,
                                        Bundle extras) {

            }

            // Provider被enable时触发此函数，比如GPS被打开
            @Override
            public void onProviderEnabled(String provider) {

            }

            // Provider被disable时触发此函数，比如GPS被关闭
            @Override
            public void onProviderDisabled(String provider) {

            }

            // 当坐标改变时触发此函数，如果Provider传进相同的坐标，它就不会被触发
            @Override
            public void onLocationChanged(Location location) {

            }
        };

        if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                    && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                T.showShort("请开启定位权限");
                return null;
            }
            Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, locationListener);
            if (location != null) {
                // 经度
                lat = location.getLatitude();
                // 纬度
                lng = location.getLongitude();
                L.e("NetWork：" + "lat = " + lat + "\nlng = " + lng);
            }
        }

        return new Double[]{lat, lng};
    }


}
